<template>
  <div>
    <van-list
      class="goods-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      offset="30"
    >
      <div
        class="goods-list-item"
        v-for="(item,index) in goods"
        :key="index"
        @click="goDetail('/home/goodsinfo/',item.id)"
      >
        <div class="img">
          <img
            v-lazy="item.img_url"
            alt=""
          >
        </div>
        <h2>{{item.title}}</h2>
        <div class="info">
          <div class="price">
            <span class="new">现价￥{{item.sell_price}}</span>
            <span class="old">原价￥{{item.market_price}}</span>
          </div>
          <div class="sell">
            <span>热卖中</span>
            <span>剩余{{item.stock_quantity}}件</span>
          </div>
        </div>
      </div>
    </van-list>

  </div>
</template>
<script>
export default {
  data: () => ({
    page: 0,
    pageSize: 5,
    goods: [],
    loading: false,
    finished: false,
    count: 0,
    hasMore: false
  }),
  created() {
    // this.getGoods()
  },
  methods: {
    async onLoad() {
      // 异步更新数据
      setTimeout(async () => {
        await this.getGoods()

        // 加载状态结束
        this.loading = false

        // 数据全部加载完成
        if (this.goods.length >= this.count) {
          this.finished = true
        }
      }, 50)
    },
    async getGoods() {
      if (this.hasMore !== false) return
      this.page++
      const res = await this.$http.getGoods({ page: this.page, pageSize: this.pageSize })
      this.count = res.count
      this.goods = this.goods.concat(res.message)
      this.hasMore = this.page * this.pageSize > this.count
    }
  }
}
</script>
<style lang="less">
.goods-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 10px;
  .goods-list-item {
    width: 48%;
    border: 1px solid #eee;
    padding: 6px;
    box-sizing: border-box;
    margin-top: 10px;
    .img {
      height: 156px;
    }
    h2 {
      font-size: 16px;
    }
    img {
      width: 100%;
    }
    .price {
      font-size: 12px;
      .new {
        color: red;
        margin-right: 20px;
      }
      .old {
        text-decoration: line-through;
      }
    }
    .sell {
      font-size: 12px;
      display: flex;
      justify-content: space-between;
      margin-top: 8px;
    }
  }
  .van-list__loading,
  .van-list__finished-text {
    width: 100%;
  }
}
</style>